<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>百宝箱</title>
        <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">

        <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>

        <script type="text/javascript" src="/webapp/js/common/jquery.global.settings.js"></script>

        <script type="text/javascript" src="/webapp/js/common/config.js"></script>

    </head>
    <body>
        <div class="easyui-container-fluid">
            <div class="easyui-panel" style="border:none;">
                <div class="nav" id="nav"></div>
                <div class="easyui-panel" style="border:none;">
                    <form id="ff">
                        <div style="margin-bottom:20px;">
                     	    <span>
                             <label class="item-label">年份：</label>
                             <input type="text" class="easyui-combobox" name="year" id="year" style="width:100px;"/>
                     	    </span>
                            <span id="monthLabel">
	                         <label class="item-label">月份：</label>
	                         <input type="text" class="easyui-combobox" name="month" id="month" style="width:100px;"/></span>
                            <span id="weekLabel">
	                         <label class="item-label">周数：</label>
	                         <input type="text" class="easyui-combobox" name="week" id="week" style="width:100px;"/></span>
                            <label class="item-label">套餐状态:</label>
                            <input id="comboFlag" class="easyui-switchbutton" style="width:80px;height:30px;">
                            <a href="javascript:void(0);" style="margin-left:40px;width:100px;" class="easyui-linkbutton c8" onclick="onlineModeNone()">处理空型号</a>
                            <label class="item-label">品类状态:</label>
                            <input id="categoryFlag" class="easyui-switchbutton" checked style="width:80px;height:30px;">
                            <a href="javascript:void(0);" style="margin-left:40px;width:120px;" class="easyui-linkbutton c8" onclick="disableDispute()">禁用空型号数据</a>
                        </div>
                        <div>
                            <label class="item-label">品类：</label>
                            <input type="text" id="categorySelect" name="categoryCode" class="easyui-combobox" data-options="panelHeight:'150'" style="width:100px;"/>
                            <a href="javascript:void(0);" style="margin-left:40px;width:100px;" class="easyui-linkbutton c8" onclick="setCityCode()">设置城市码</a>
                            <a href="javascript:void(0);" style="margin-left:40px;width:130px;" class="easyui-linkbutton c8" onclick="cbStatus()">允许处理套餐</a>
                        </div>
                        <br>
                        <div style="margin-left:45px">
                            <label class="item-label">品类(供[多])：</label>
                            <input type="text" id="categoryStr" class="easyui-textbox" style="width:200px;"/>
                            <input type="text" id="upPercentage" class="easyui-textbox" style="width:60px;"/> %
                            <br>
                            <br>
                            <label class="item-label">品牌(供[单])：</label>
                            <input type="text" id="brandStr" class="easyui-textbox" style="width:200px;"/>
                            <a href="javascript:void(0);" style="width:100px;" class="easyui-linkbutton c8" onclick="siphonCount()">供销型号</a>
                            <a href="javascript:void(0);" style="width:50px;" class="easyui-linkbutton c6" onclick="cancel()">撤销</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <br><br>
        <div style="margin-left:48px">
            <hr style=" width:10%; display:inline-block"/>
            同步SQL server之华丽的分割线
            <hr style=" width:10%; display:inline-block"/>
            <br>
            <br>
            <a href="javascript:void(0);" style="width:100px;" class="easyui-linkbutton c0" onclick="synBrand()">同步品牌</a>
            <a href="javascript:void(0);" style="margin-left:40px;width:100px;" class="easyui-linkbutton c0" onclick="synChannel()">同步渠道</a>
            <label class="item-label">品类：</label>
            <input type="text" id="categorySelectSql" class="easyui-combobox" data-options="panelHeight:'150'" style="width:100px;"/>
            <a href="javascript:void(0);" style="margin-left:40px;width:100px;" class="easyui-linkbutton c0" onclick="synModel()">同步型号</a>
            <div style="padding:5px 0;text-align:center;" id="progressbarDiv">
                <div id="progressbar" class="easyui-progressbar" style="width:400px;margin:0 auto;display:none;"></div>
            </div>
        </div>
        <script type="text/javascript">
            $(function () {
                // $("#categoryStr").textbox("setValue", "22,23,32,33,34,39");
                // $("#brandStr").textbox("setValue", "03752");
                setNav();
                initMonthWeek("monthLabel", "weekLabel");
                $("#datagrid").datagrid({
                    rownumbers: true
                });
                aRecords("百宝箱","/webapp/pages/product/audit/onlineSalesDetail")
            });

            function onlineModeNone() {
                var reportType = window.parent.workMode.toUpperCase();
                if (reportType != "ONLINE") {
                    $.messager.alert("错误", "该功能只针对电商数据有效", "error");
                    return;
                }
                var data = getFormData(document.getElementById("ff"));
                data.reportType = reportType;
                data.comboFlag = $("#comboFlag").switchbutton("options").checked == true ? 'true' : 'false';
                $.messager.confirm("提示", "您确定要对" + $('#year').combobox('getValue') + "年" + $('#week').combobox('getValue') + "周的数据执行此操作吗?", function (r) {
                    if (r) {
                        $.messager.confirm("提示", "您真的确定吗?", function (o) {
                            if (o) {
                                $.ajax({
                                    url: config.url + "/workshop/onlineModeNone",
                                    type: "get",
                                    data: transfer(data),
                                    dataType: "json",
                                    xhrFields: {withCredentials: true},
                                    beforeSend: function () {
                                        $.messager.progress({
                                            title: "请稍等",
                                            interval: 50,
                                            text: "数据加载中..."
                                        });
                                    },
                                    success: function (data) {
                                        if (data.code == "200") {
                                            $.messager.alert("成功", data.msg, "info");
                                        } else {
                                            $.messager.alert("错误", data.msg, "error");
                                        }
                                        $.messager.progress("close");
                                    },
                                    error: function (data) {
                                        console.log(data);
                                        $.messager.progress("close");
                                    }
                                });
                            }
                        });
                    }
                });
            }

            function disableDispute() {
                var reportType = window.parent.workMode.toUpperCase();
                if (reportType != "ONLINE") {
                    $.messager.alert("错误", "该功能只针对电商数据有效", "error");
                    return;
                }
                var data = getFormData(document.getElementById("ff"));
                data.reportType = reportType;
                data.categoryFlag = $("#categoryFlag").switchbutton("options").checked == true ? 'true' : 'false';
                data.comboFlag = $("#comboFlag").switchbutton("options").checked == true ? 'true' : 'false';

                $.messager.confirm("提示", "您确定要禁用争议数据吗?", function (r) {
                    if (r) {
                        $.ajax({
                            url: config.url + "/workshop/disableDispute",
                            type: "post",
                            data: transfer(data),
                            dataType: "json",
                            xhrFields: {withCredentials: true},
                            beforeSend: function () {
                                $.messager.progress({
                                    title: "请稍等",
                                    interval: 50,
                                    text: "数据加载中..."
                                });
                            },
                            success: function (data) {
                                if (data.code == "200") {
                                    $.messager.alert("成功", data.msg, "info");
                                } else {
                                    $.messager.alert("错误", data.msg, "error");
                                }
                                $.messager.progress("close");
                            },
                            error: function (data) {
                                console.log(data);
                                $.messager.progress("close");
                            }
                        });
                    }
                });

            }


            function cbStatus() {
                $.messager.confirm("提示", "您确定和当前套餐不是同一期吗?操作不可逆", function (o) {
                    if (o) {
                        $.ajax({
                            url: config.url + "/sales/detail/cbStatus",
                            type: "get",
                            dataType: "json",
                            xhrFields: {withCredentials: true},
                            beforeSend: function () {
                                $.messager.progress({
                                    title: "请稍等",
                                    interval: 50,
                                    text: "数据加载中..."
                                });
                            },
                            success: function (data) {
                                if (data.code == "200") {
                                    $.messager.alert("成功", data.msg, "info");
                                } else {
                                    $.messager.alert("错误", data.msg, "error");
                                }
                                $.messager.progress("close");
                            },
                            error: function (data) {
                                console.log(data);
                                $.messager.progress("close");
                            }
                        });
                    }
                });
            }

            function setCityCode() {
                var reportType = window.parent.workMode.toUpperCase();
                if (reportType == "ONLINE") {
                    $.messager.alert("错误", "该功能不支持电商数据", "error");
                    return;
                }
                var data = getFormData(document.getElementById("ff"));
                data.reportType = reportType;
                $.messager.confirm("提示", "您确定要添加城市码吗?", function (o) {
                    if (o) {
                        $.ajax({
                            url: config.url + "/workshop/setCityCode",
                            type: "post",
                            data: transfer(data),
                            dataType: "json",
                            xhrFields: {withCredentials: true},
                            beforeSend: function () {
                                $.messager.progress({
                                    title: "请稍等",
                                    interval: 50,
                                    text: "数据加载中..."
                                });
                            },
                            success: function (data) {
                                if (data.code == "200") {
                                    $.messager.alert("成功", data.msg, "info");
                                } else {
                                    $.messager.alert("错误", data.msg, "error");
                                }
                                $.messager.progress("close");
                            },
                            error: function (data) {
                                console.log(data);
                                $.messager.progress("close");
                            }
                        });
                    }
                });
            }

            $(function () {
                var dtd = $.Deferred();
                $.ajax({
                    url: config.url + "/category/getAll",
                    type: "get",
                    xhrFields: {withCredentials: true},
                    success: function (data) {
                        if (data) {
                            data.unshift({"name": "全部", "code": ""});
                            $("#categorySelect").combobox({
                                "textField": "name",
                                "valueField": "code",
                                "panelHeight": 150,
                                "data": data,
                                onChange: function (newValue, oldValue) {
                                    var val = $(this).combobox("getText");
                                    var textField = $(this).combobox("options").textField;
                                    var matchSize = $(this).combobox("getData").filter(function (item) {
                                        return item[textField] == val;
                                    }).length;
                                    if (matchSize != 1) {
                                        return false;
                                    }
                                    if (newValue == "") return false;
                                    if (onchange && typeof onchange === 'function') {
                                        onchange.call(this, newValue);
                                    }
                                }
                            });
                            $("#categorySelectSql").combobox({
                                "textField": "name",
                                "valueField": "code",
                                "panelHeight": 150,
                                "data": data,
                                onChange: function (newValue, oldValue) {
                                    var val = $(this).combobox("getText");
                                    var textField = $(this).combobox("options").textField;
                                    var matchSize = $(this).combobox("getData").filter(function (item) {
                                        return item[textField] == val;
                                    }).length;
                                    if (matchSize != 1) {
                                        return false;
                                    }
                                    if (newValue == "") return false;
                                    if (onchange && typeof onchange === 'function') {
                                        onchange.call(this, newValue);
                                    }
                                }
                            });
                        }
                        dtd.resolve();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        dtd.resolve();
                    }
                })
                dtd.promise();
            });

            function siphonCount() {
                if (!isOfflineType()) {
                    alert("该功能目前只支持线下模式");
                    return;
                }

                var reportType = window.parent.workMode.toUpperCase();
                var data = {};
                var ff = getFormData(document.getElementById("ff"));
                data.year = ff['year'];
                data.reportType = reportType;
                if (isMonthType()) {
                    data.month = ff['month'];
                } else {
                    data.week = ff['week'];
                }
                data.categoryCodes = $("#categoryStr").textbox("getValue");
                data.brandCode = $("#brandStr").textbox("getValue");
                data.upPercentage = $("#upPercentage").textbox("getValue");
                $.messager.confirm("提示", "您确定要执行"+data.week+"周的供销型号吗?", function (o) {
                    if (o) {
                        $.ajax({
                            url: config.url + "/workshop/siphonCount",
                            type: "post",
                            data: data,
                            dataType: "json",
                            xhrFields: {withCredentials: true},
                            beforeSend: function () {
                                $.messager.progress({
                                    title: "请稍等",
                                    interval: 50,
                                    text: "数据加载中..."
                                });
                            },
                            success: function (data) {
                                $.messager.progress("close");
                                if (data.code == "200") {
                                    $.messager.show({
                                        title: '提示',
                                        msg: data.msg,
                                        timeout: 0,
                                        showType: 'slide'
                                    });
                                } else {
                                    $.messager.alert("错误", data.msg, "error");
                                }
                            },
                            error: function () {
                                $.messager.progress("close");
                            }
                        });
                    }
                });
            }

            function cancel() {
                var reportType = window.parent.workMode.toUpperCase();
                var data = {};
                var ff = getFormData(document.getElementById("ff"));
                data.year = ff['year'];
                data.reportType = reportType;
                if (isMonthType()) {
                    data.month = ff['month'];
                } else {
                    data.week = ff['week'];
                }
                data.categoryCodes = $("#categoryStr").textbox("getValue");
                data.brandCode = $("#brandStr").textbox("getValue");
                $.messager.confirm("提示", "您确定要撤销"+data.week+"周的供销型号吗?", function (o) {
                    if (o) {
                        $.ajax({
                            url: config.url + "/workshop/cancel",
                            type: "post",
                            data: data,
                            dataType: "json",
                            xhrFields: {withCredentials: true},
                            beforeSend: function () {
                                $.messager.progress({
                                    title: "请稍等",
                                    interval: 50,
                                    text: "数据撤销中..."
                                });
                            },
                            success: function (data) {
                                $.messager.progress("close");
                                if (data.code == "200") {
                                    $.messager.alert("提示", data.msg, "info");
                                } else {
                                    $.messager.alert("错误", data.msg, "error");
                                }
                            },
                            error: function () {
                                $.messager.progress("close");
                            }
                        });
                    }
                });
            }
        </script>
    </body>

    <script type="text/javascript">
        function synBrand() {
            $.messager.confirm("提示", "您确定要同步品牌表吗?", function (r) {
                if (r) {
                    $.ajax({
                        url: window.config.synHost+"brand",
                        type: "POST",
                        data: {access_token: e},
                        dataType: "json",
                        beforeSend: function () {
                            $.messager.progress({
                                title: "请稍等",
                                interval: 30,
                                text: "数据同步中..."
                            });
                        },
                        success: function (data) {
                            $.messager.show({
                                title: '提示',
                                msg: data.msg,
                                timeout: 0,
                                showType: 'slide'
                            });
                            $.messager.progress("close");
                        },
                        error: function () {
                            $.messager.progress("close");
                            $.messager.alert("错误", "系统异常", "error");
                        }
                    });
                }
            });
        }

        function synChannel() {
            $.messager.confirm("提示", "您确定要同步渠道表吗?", function (r) {
                if (r) {
                    $.ajax({
                        url: window.config.synHost+"channel",
                        type: "POST",
                        data: {access_token: e},
                        dataType: "json",
                        beforeSend: function () {
                            $.messager.progress({
                                title: "请稍等",
                                interval: 30,
                                text: "数据同步中..."
                            });
                        },
                        success: function (data) {
                            $.messager.show({
                                title: '提示',
                                msg: data.msg,
                                timeout: 0,
                                showType: 'slide'
                            });
                            $.messager.progress("close");
                        },
                        error: function () {
                            $.messager.progress("close");
                            $.messager.alert("错误", "系统异常", "error");
                        }
                    });
                }
            });
        }

        function synModel() {
            var categorySelectSql = $("#categorySelectSql").combobox('getValue');
            var content;
            if (categorySelectSql == "") {
                categorySelectSql = "ALL";
                content = "您确定要同步【全品类】型号表吗?";
            } else {
                let category = $('#categorySelectSql').combobox('getText');
                content = "您确定要同步[" + category + "]型号表吗?";
            }
            $.messager.confirm("提示", content, function (r) {
                if (r) {
                    $.ajax({
                        url: window.config.synHost+"Code/" + categorySelectSql,
                        type: "post",
                        data: {access_token: e},
                        dataType: "json",
                        beforeSend: function () {
                            $.messager.progress({
                                title: "请稍等",
                                interval: 40,
                                text: "数据同步中..."
                            });
                        },
                        success: function (data) {
                            $.messager.progress("close");
                            $.messager.show({
                                title: '提示',
                                msg: data.msg,
                                timeout: 0,
                                showType: 'slide'
                            });

                            if (categorySelectSql == "ALL") {
                                showProgress()
                            }
                        },
                        error: function () {
                            $.messager.progress("close");
                            $.messager.alert("错误", "系统异常", "error");
                        }
                    });
                }
            });
        }

        function showProgress() {
            $("#progressbar").show();//显示div
            $.ajax({
                url: window.config.synHost+"Monitor",
                type: "post",
                data: {'access_token':e},
                dataType: "json",
                success: function (data) {
                    if ($('#progressbar').progressbar('getValue') < 100) {
                        $('#progressbar').progressbar('setValue', ~~data.progress);
                        if (data.progress >= 100) {
                            $.messager.show({
                                title: '提示',
                                msg: "全部型号同步完成",
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            setTimeout(showProgress, 2000);
                        }
                    }
                },
                error: function (data) {
                    console.log(data);
                }
            });

        }
    </script>
</html>